package com.wenhai.xiaomage.spring.ioc.overview.dependency.lookup;

import com.wenhai.xiaomage.spring.ioc.overview.dependency.repository.UserRepository;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;

/**
 * @Author:wenhai
 * @Description: 依赖注入
 * @Date:2020/3/4
 */
public class DependencyInjectionDemo {
    public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:/META-INF/dependency-injection-context.xml");
        // 自定义 Bean
        UserRepository userRepository = (UserRepository) beanFactory.getBean("userRepository");
//        System.out.println(userRepository.getUsers());
        // 依赖注入(内建依赖)
        System.out.println(userRepository.getBeanFactory());
        System.out.println(userRepository.getObjectFactory());

        // 注入内部Bean
        System.out.println("userRepository.getObjectFactory() == beanFactory: " + (userRepository.getObjectFactory().getObject() == beanFactory));

        // 容器内建Bean
        Environment environment = beanFactory.getBean(Environment.class);
        System.out.println(environment);




    }
    private static void whoIsContainer(UserRepository userRepository,BeanFactory beanFactory){
        // 这个表达式为什么不成立
        System.out.println(userRepository.getBeanFactory() == beanFactory);

        // ApplicationContext is BeanFactory
    }

}
